<template>
	<view class="index" :style="{ '--color': vuex_config.themeColor1 }">
		<template v-if="currentId > 0">
			<u-sticky><u-tabs :list="list" :is-scroll="false" :active-color="vuex_config.themeColor1" :bold="false" :current="current" @change="change"></u-tabs></u-sticky>
			<redenvelopes-list ref="page_view" v-if="currentId == 1" :type="1" />
			<redenvelopes-list ref="page_view" v-else-if="currentId == 2" :type="2" @useProdEnvelope="useProdEnvelope" />
			<record-list ref="page_view" v-else />
		</template>
		<sww-login></sww-login>
	</view>
</template>

<script>
import redenvelopesList from './components/redenvelopesList.vue';
import recordList from './components/recordList.vue';
import { pageShare } from '@/mixins/pageShare.js';
import { uniModal } from '@/utils/uniPromise';
export default {
	mixins: [pageShare],
	components: {
		redenvelopesList,
		recordList
	},
	data() {
		return {
			list: [
				{
					name: '我的红包',
					id: 2
				},
				{
					name: '收发红包记录',
					id: 3
				}
			],
			current: 0,
			currentId: null,
			orderProdObj: {},
			initVisible: false
		};
	},
	onLoad() {
		this.init();
	},
	onShow() {
		if (!this.initVisible) {
			if (this.$refs.page_view && this.$refs.page_view.$onShow) {
				this.$refs.page_view.$onShow();
			}
		} else {
			this.initVisible = true;
		}
	},
	onReachBottom: function() {
		this.$refs.page_view.$onReachBottom();
	},
	onShareAppMessage: function(res) {
		if (res.from == 'button' && this.currentId == 2) {
			// 小程序点击分享按钮转赠
			return this.giveShare();
		} else {
			this.shareParams = {
				title: '',
				desc: '',
				path: '',
				imgUrl: ''
			};
			return this.getShareAppMessage();
		}
	},
	methods: {
		init() {
			this.$login('init', null, 'goback').then(() => {
				let current = 0;
				// if (this.vuex_user.type == 2) {
				// 	current = 1;
				// 	this.list.unshift({ name: '平台红包', id: 1 });
				// }
				this.current = current;
				this.currentId = 2;
			});
		},
		goback() {
			this.uniBack();
		},
		change(index) {
			this.current = index;
			this.currentId = this.list[index].id;
		},
		// 赠送分享
		giveShare() {
			return new Promise((resolve, reject) => {
				let componentParams = this.$refs.page_view.shareParams;
				let params = {
					receive_id: componentParams.receive_id,
					mode: componentParams.mode,
					num: componentParams.num,
					title: componentParams.title,
					relay_num: componentParams.relay_num
				};
				this.$loading();
				this.$resPost('AddUsersRedRecord', params)
					.then(({ data }) => {
						// 调用转发赠送接口
						this.shareParams = {
							title: componentParams.title || '恭喜发财，大吉大利',
							path: 'package/redEnvelopes/receive',
							query: { id: data },
							imgUrl: componentParams.img
						};
						resolve(this.getShareAppMessage());
						this.$hide();
					})
					.catch(() => {
						this.shareParams = {
							title: '',
							desc: '',
							path: '',
							imgUrl: ''
						};
						resolve();
					});
			});
		},
		// 兑换产品
		useProdEnvelope(obj) {
			this.$resPost('GetProductDetailed', {
				pk_id: obj.fk_pro_id
			})
				.then(({ data }) => {
					if (!data.pk_id || !data.product_stock[0] || data.is_online != 1 || data.status != 1) {
						return Promise.reject();
					}
					let prodForm = {
						...data
					};

					let specActItem = prodForm.product_stock[0];
					this.orderProdObj = {
						product_img: prodForm.logo,
						express_num: prodForm.express_num, // 多少起免邮费
						express_price: prodForm.express_price, // 快递费用
						product_freight: prodForm.product_freight || [], // 运费模板
						num: obj.num,
						price: specActItem.amount,
						product_id: prodForm.pk_id,
						product_name: prodForm.name,
						fk_attr_id: specActItem.pk_id, // 属性库存id
						standard: specActItem.name, // 属性库存名称
						amount: obj.amount
					};
					this.$hide();
					this.$linkTo(`/package/product/redEnvelope/order?receiveId=${obj.receiveId}&envelopeId=${obj.envelopeId}`);
				})
				.catch(() => {
					uniModal({
						title: '商品已下架',
						showCancel: false,
						confirmText: '我知道了'
					});
				});
		}
	}
};
</script>
<style>
page {
	background-color: #f5f5f5;
}
</style>
<style lang="scss" scoped>
.index {
}
</style>
